<template>
	<view class="preview">
		<swiper class="swiper" circular :current="currentIndex" @change="changeSwiper">
			<swiper-item v-for="(item, index) in classList" :key="item?._id">
				<image class="pic" v-if="readImgs.includes(index)" @click="changeMaskState" :src="item.picture"
					mode="aspectFill">
				</image>
			</swiper-item>
		</swiper>

		<!-- <image class='pic' @click="changeMaskState" :src="classList[0]?.picture" mode="aspectFill"></image> -->

		<view class="mask" v-if="maskState">
			<view class="return" @click="goBack">
				<uni-icons type="back" color="#fff" size="20"></uni-icons>
			</view>
			<view class="datetime">
				<view class="itemNum"><text>7/12</text></view>
				<view class="time"><uni-dateformat :date="new Date()" format="hh:mm"></uni-dateformat></view>
				<view class="date"><uni-dateformat :date="new Date()" format="MM月dd日"></uni-dateformat></view>
			</view>
			<view class="footer">
				<view class="box" @click="clickInfo">
					<uni-icons type="info" size="28"></uni-icons>
					<view class="text">信息</view>
				</view>

				<view class="box" @click="clickScore">
					<uni-icons type="star" size="28"></uni-icons>
					<view class="text">{{currentInfo?.score}}分</view>
				</view>

				<view class="box" @click="clickDownload">
					<uni-icons type="download" size="23"></uni-icons>
					<view class="text">下载</view>
				</view>
			</view>
		</view>


		<uni-popup ref="infoPopup" type="bottom">
			<view class="infoPopup">
				<view class="infoHeader"><text>壁纸信息</text></view>
				<view class="infoContent">
					<view class="infoItem">
						<text class="label">壁纸ID:</text>
						<view class="value">{{currentInfo._id}}</view>
					</view>
					<view class="infoItem">
						<text class="label">发布者:</text>
						<view class="value">{{currentInfo.nickname}}</view>
					</view>
					<view class="infoItem">
						<text class="label">评分:</text>
						<view class="value">
							<uni-rate readonly touchable :value="currentInfo?.score" size="16" />
							<text class="score">{{currentInfo?.score}}分</text>
						</view>
					</view>
					<view class="infoItem">
						<text class="label">摘要:</text>
						<view class="value">{{currentInfo.description}}</view>
					</view>
					<view class="infoItem">
						<text class="label">标签:</text>
						<view class="value">
							<view class="tab" v-for="tab in currentInfo.tabs">{{tab}}</view>
						</view>
					</view>
				</view>
				<view class="copyright">
					声明：本图片来用户投稿，非商业使用，用于免费学习交流，如侵犯了您的权益，您可以拷贝壁纸ID举报至平台，邮箱513894357@qq.com，管理将删除侵权壁纸，维护您的权益。
				</view>
			</view>
		</uni-popup>

		<uni-popup ref="scorePopup" :is-mask-click="false">
			<view class="scorePopup">
				<view class="scoreHeader">壁纸评分</view>
				<view class="scoreContent">
					<uni-rate v-model="userScore" allowHalf touchable disabled-color="#FFCA3E" />
					<text class="text">{{userScore}}分</text>
				</view>
				<view class="scoreFooter">
					<button class="scoreSubmitBtn" @click="submitScore">确认评分</button>
				</view>
				<uni-icons class="closeScoreBtn" type="closeempty" @click="closeScore"></uni-icons>
			</view>
		</uni-popup>

	</view>
</template>

<script setup>
	import {
		onMounted
	} from 'vue'
	import {
		apiDetailWall
	} from "@/api/apis.js"
	const imageUrl = ref('')
	const maskState = ref(true)
	const currentId = ref('')
	const currentInfo = ref(null)
	const classList = ref([])
	const readImgs = ref([]);
	const currentIndex = ref(null)
	const infoPopup = ref(null)
	const currentStarNum = ref(0)
	const userScore = ref(0)

	const scorePopup = ref(null)
	const isScore = ref(false)

	onLoad(async (e) => {
		currentId.value = e.id
		console.log("e:", e)
		if (e.type = 'share') {
			const res = await apiDetailWall({
				id: currentId.value
			})
			classList.value = res.data.map(item => {
				return {
					...item,
					picture: item.smallPicurl.replace("_small.webp", ".jpg")
				}
			})
			console.log("classList:", classList)
		}
		currentIndex.value = classList.value.findIndex(item => item._id == currentId.value)
		currentInfo.value = classList.value[currentIndex.value]
		userScore.value = parseFloat(currentInfo.value.score)
		console.log("currentInfo:", currentInfo)
		console.log("userScore:", userScore.value)
		readImgsFun()
	})

	function readImgsFun() {
		readImgs.value.push(
			currentIndex.value <= 0 ? classList.value.length - 1 : currentIndex.value - 1,
			currentIndex.value,
			currentIndex.value >= classList.value.length - 1 ? 0 : currentIndex.value + 1
		)
		readImgs.value = [...new Set(readImgs.value)];
		console.log("readImgs:", readImgs)
	}

	function changeMaskState() {
		maskState.value = !maskState.value
	}

	function goBack() {
		uni.navigateBack({
			success: () => {

			},
			fail: (err) => {
				uni.reLaunch({
					url: "/pages/index/index"
				})
			}
		})
	}

	function changeSwiper() {

	}


	function clickInfo() {
		infoPopup.value.open()
	}

	function clickScore() {
		console.log("currentInfo:", currentInfo.value)
		if (currentInfo.value.score) {
			isScore.value = true;
			console.log("userScore:", userScore.value)
			// userScore.value = currentInfo.value.score
		}
		scorePopup.value.open()
	}
	
	function submitScore(){
		currentInfo.score = userScore.value
		closeScore()
	}
	
	function closeScore() {
		scorePopup.value.close();
		userScore.value = 0;
		isScore.value = false;
	}
	
	const clickDownload = async () => {
	
		// #ifdef H5
		uni.showModal({
			content: "请长按保存壁纸",
			showCancel: false
		})
		// #endif
	
		// #ifndef H5
		try {
	
			uni.showLoading({
				title: "下载中...",
				mask: true
			})
			let {
				classid,
				_id: wallId
			} = currentInfo.value;
			let res = await apiWriteDownload({
				classid,
				wallId
			})
			if (res.errCode != 0) throw res;
			uni.getImageInfo({
				src: currentInfo.value.picture,
				success: (res) => {
					uni.saveImageToPhotosAlbum({
						filePath: res.path,
						success: (res) => {
							uni.showToast({
								title: "保存成功，请到相册查看",
								icon: "none"
							})
						},
						fail: err => {
							if (err.errMsg == 'saveImageToPhotosAlbum:fail cancel') {
								uni.showToast({
									title: '保存失败，请重新点击下载',
									icon: "none"
								})
								return;
							}
							uni.showModal({
								title: "授权提示",
								content: "需要授权保存相册",
								success: res => {
									if (res.confirm) {
										uni.openSetting({
											success: (setting) => {
												console.log(
													setting);
												if (setting
													.authSetting[
														'scope.writePhotosAlbum'
													]) {
													uni.showToast({
														title: "获取授权成功",
														icon: "none"
													})
												} else {
													uni.showToast({
														title: "获取权限失败",
														icon: "none"
													})
												}
											}
										})
									}
								}
							})
						},
						complete: () => {
							uni.hideLoading();
						}
					})
	
				}
			})
	
		} catch (err) {
			console.log(err);
			uni.hideLoading();
		}
		// #endif
	}
	
	
	
</script>

<style lang="scss" scoped>
	.preview {
		width: 100%; // 设置容器宽度
		height: 100vh;
		overflow: hidden; // 防止图片溢出
		position: relative;


		.swiper {
			width: 100%;
			height: 100%;

			.pic {
				width: 100%; // 图片宽度填充容器
				height: 100%; // 图片高度填充容器
			}
		}

		.mask {
			&>view {
				position: absolute;
				left: 0;
				margin: auto;
				color: #fff;
				right: 0;
				width: fit-content;
			}

			.return {
				width: 38px;
				height: 38px;
				background: rgba(0, 0, 0, 0.5);
				left: 30rpx;
				margin-left: 0;
				border-radius: 100px;
				top: 5%;
				backdrop-filter: blur(10rpx);
				border: 1rpx solid rgba(255, 255, 255, 0.3);
				display: flex;
				align-items: center;
				justify-content: center;
			}

			.datetime {
				top: 15%;
				width: 100%;
				display: flex;
				flex-direction: column;
				justify-content: center;
				align-items: center;
				color: white;

				.itemNum {
					font-size: 35rpx;
					background: rgba(0, 0, 0, 0.3);
					border-radius: 35rpx;
					width: 150rpx;
					height: 60rpx;
					backdrop-filter: blur(10rpx);
					display: flex;
					align-items: center;
					justify-content: center;
				}

				.time {
					font-size: 135rpx;
					text-shadow: 0 4rpx rgba(0, 0, 0, 0.3);
				}

				.date {
					font-size: 35rpx;
					text-shadow: 0 4rpx rgba(0, 0, 0, 0.3);
				}
			}

			.footer {
				background: rgba(255, 255, 255, 0.8);
				bottom: 10vh;
				width: 65vw;
				height: 120rpx;
				border-radius: 120rpx;
				color: #000;
				display: flex;
				justify-content: space-around;
				align-items: center;
				box-shadow: 0 2rpx 0 rgba(0, 0, 0, 0.1);
				backdrop-filter: blur(20rpx);
			}
		}

		.infoPopup {
			background-color: #fff;
			padding: 30rpx;
			width: 100%;
			border-radius: 30rpx 30rpx 0 0;
			overflow: hidden;

			.infoHeader {
				display: flex;
				justify-content: center;
				color: $text-font-color-3;
				margin-bottom: 35rpx;
				font-size: 25rpx;
			}

			.infoContent {
				display: flex;
				flex-direction: column;
				gap: 35rpx;

				.infoItem {
					display: flex;
					justify-content: center;
					gap: 20rpx;

					.label {
						width: 140rpx;
						color: $text-font-color-3;
						text-align: right;
						font-size: 30rpx;
					}

					.value {
						width: 580rpx;
						display: flex;
						align-items: center;
						gap: 10rpx;
						font-size: 32rpx;

						.starInfo {}

						.score {
							color: gold;
							font-size: 30rpx;
						}

						.tab {
							font-size: 20rpx;
							border-radius: 30rpx;
							border: 1rpx solid $brand-theme-color;
							color: $brand-theme-color;
							padding: 8rpx;
						}
					}
				}
			}

			.copyright {
				font-size: 28rpx;
				padding: 20rpx;
				background: #F6F6F6;
				color: #666;
				border-radius: 10rpx;
				margin: 20rpx 0;
				line-height: 1.6em;
			}
		}

		.scorePopup {
			background: #fff;
			padding: 30rpx;
			width: 70vw;
			border-radius: 30rpx;
			position: relative;

			.scoreHeader {
				display: flex;
				justify-content: center;
				color: $text-font-color-3;
				margin-bottom: 55rpx;
				font-size: 25rpx;
			}

			.scoreContent {
				display: flex;
				justify-content: center;
				align-items: center;
				font-size: 35rpx;
				gap: 10rpx;
				margin-bottom: 45rpx;
			}

			.scoreFooter {
				.scoreSubmitBtn {
					width: 185rpx;
					height: 70rpx;
					line-height: 70rpx;
					font-size: 25rpx;
					background: none;
					border: 1rpx solid dimgray;
				}
			}

			.closeScoreBtn {
				position: absolute;
				right: 7%;
				top: 10%;
				:deep(){
					.uni-icons{
						color: $text-font-color-3;
					}
				}
			}
		}

	}
</style>